<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="openid" size="mini" prop="openId">
        <el-input v-model="queryParams.openId" placeholder="请输入微信openid" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户ID" size="mini" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入用户ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户昵称" size="mini" prop="nikeName">
        <el-input v-model="queryParams.nikeName" placeholder="请输入用户昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" size="mini" prop="phoneNumber">
        <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="创建时间" size="mini" prop="createTime">
        <el-date-picker clearable v-model="queryParams.createTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="最后更新时间"size="mini" prop="updateTime">
        <el-date-picker clearable
          v-model="queryParams.updateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后更新时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="生日" size="mini" prop="birthday">
        <el-date-picker clearable v-model="queryParams.birthday" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择生日">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="学历" size="mini" prop="education">
        <el-select v-model="queryParams.education" placeholder="请选择学历" clearable style="width: 180px;">
          <el-option v-for="dict in sys_teenage_edu" :key="dict.value" :label="dict.label" :value="dict.value">
            <span>{{ dict.label }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="职业状态" size="mini" prop="employmentStatus">
        <el-select v-model="queryParams.employmentStatus" placeholder="请选择职业状态" clearable style="width: 180px;">
          <el-option v-for="dict in sys_teenage_status" :key="dict.value" :label="dict.label" :value="dict.value">
            <span>{{ dict.label }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所在地" size="mini" prop="location">
        <el-input v-model="queryParams.location" placeholder="请输入所在地" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teenage:teenage:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="lock"
          size="mini"
          style="width: 85px"
          :disabled="single"
          @click="handleStatusUpdate(0)"
          v-hasPermi="['teenage:teenage:edit']"
        >封禁</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="unlock"
          size="mini"
          style="width: 85px"
          :disabled="single"
          @click="handleStatusUpdate(1)"
          v-hasPermi="['teenage:teenage:edit']"
        >解封</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['teenage:teenage:remove']">删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teenage:teenage:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teenageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="微信openid" align="center" prop="openId" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户昵称" align="center" prop="nikeName" />
      <el-table-column label="性别" align="center" prop="gender">
        <template #default="scope">
          <span>{{ scope.row.gender == 1 ? '男' : (scope.row.gender == 2 ? '女' : '-') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phoneNumber" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="{ row }">
          <span>{{ parseTime(row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新时间" align="center" prop="updateTime" width="180">
        <template v-slot="{ row }">
          <span>{{ parseTime(row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生日" align="center" prop="birthday" width="180">
        <template v-slot="{ row }">
          <span>{{ parseTime(row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户状态" align="center" prop="userStatus">
        <template v-slot="{ row }">
          <dict-tag :options="[
            { label: '正常', value: '1' },
            { label: '禁用', value: '0' }
          ]" :value="row.userStatus" />
        </template>
      </el-table-column>
      <el-table-column label="学历" align="center" prop="education">
        <template v-slot="{ row }">
          <dict-tag :options="sys_teenage_edu" :value="row.education" />
        </template>
      </el-table-column>
      <el-table-column label="职业状态" align="center" prop="employmentStatus">
        <template v-slot="{ row }">
          <dict-tag :options="sys_teenage_status" :value="row.employmentStatus" />
        </template>
      </el-table-column>
      <el-table-column label="所在地" align="center" prop="location" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="{ row }">
          <el-button
            size="mini"
            type="text"
            icon="Edit"
            @click="handleUpdate(row)"
            v-hasPermi="['teenage:teenage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="Delete"
            @click="handleDelete(row)"
            v-hasPermi="['teenage:teenage:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page="queryParams.pageNum" :limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改青年管理模块对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户昵称" prop="nikeName">
          <el-input v-model="form.nikeName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker clearable v-model="form.birthday" type="date" value-format="yyyy-MM-dd" placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-select v-model="form.education" placeholder="请选择学历" clearable>
            <el-option v-for="dict in sys_teenage_edu" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="职业状态" prop="employmentStatus">
          <el-radio-group v-model="form.employmentStatus">
            <el-radio v-for="dict in sys_teenage_status" :key="dict.value" :label="dict.value">{{ dict.label
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所在地" prop="location">
          <el-input v-model="form.location" placeholder="请输入所在地" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeenage, getTeenage, delTeenage, addTeenage, updateTeenage, updateStatusByuserid } from "@/api/teenage/teenage";
import { useDict } from '@/utils/dict'
import { ref, reactive, onMounted } from 'vue'

export default {
  name: "Teenage",
  setup() {
    const { sys_teenage_status, sys_teenage_edu } = useDict('sys_teenage_status', 'sys_teenage_edu')
    const queryParams = reactive({
      pageNum: 1,
      pageSize: 10,
      openId: null,
      userId: null,
      nikeName: null,
      phoneNumber: null,
      createTime: null,
      updateTime: null,
      birthday: null,
      userStatus: null,
      education: '',
      employmentStatus: '',
      location: null
    })

    return {
      sys_teenage_status,
      sys_teenage_edu,
      queryParams
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 青年管理模块表格数据
      teenageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询青年管理模块列表 */
    getList(paginationParams) {
      // 如果有分页参数，更新查询参数
      if (paginationParams) {
        this.queryParams.pageNum = paginationParams.page
        this.queryParams.pageSize = paginationParams.limit
      }
      
      this.loading = true
      listTeenage(this.queryParams).then(response => {
        this.teenageList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        openId: null,
        userId: null,
        nikeName: null,
        avatar: null,
        phoneNumber: null,
        createTime: null,
        updateTime: null,
        birthday: null,
        userStatus: null,
        education: null,
        employmentStatus: null,
        location: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.queryParams.education = ''
      this.queryParams.employmentStatus = ''
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset()
    //   this.open = true
    //   this.title = "添加青年管理模块"
    // },
    /** 修改按钮操作 */
    handleStatusUpdate(status) {
      const userid = this.ids[0];
      const userStatus = status;
      const actionText = status === 0 ? '封禁' : '解封';
      this.$modal.confirm('是否确认' + actionText + '该用户？').then(function() {
        return updateStatusByuserid(userid, userStatus);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(actionText + "成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids
      this.$modal.confirm('是否确认删除青年管理模块编号为"' + userIds + '"的数据项？').then(function () {
        return delTeenage(userIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   this.download('teenage/teenage/export', {
    //     ...this.queryParams
    //   }, `teenage_${new Date().getTime()}.xlsx`)
    // }
  }
}
</script>
